import os
import string

from dvb3 import frontend, dmx

class Channels:
	def __init__(self):
		self.channels = {}
		return
	def __getitem__(self, key):
		"""
		get method
		"""
		return self.channels[key]

	def has_key(self, key):
		"""
		has_key method
		"""
		return self.channels.has_key(key)
	
	def keys(self):
		return self.channels.keys()

	def values(self):
		return self.channels.values()

	def read(self, fd):
		"""
		Read the channel list from the file handle given
		"""
		self.channels = {}
		for line in fd.readlines():
			t = string.split(line, ':')
			if len(t) == 13:
				self.channels[t[0]] = t

	def get_parameter(self, key):
		"""
		Create a parameter for tuning in the front end
		"""
		c = self.channels[key]
		p = frontend.OFDMParameters()
		p.frequency = int(c[1])

		p.inversion = frontend.INVERSION_AUTO

		p.bandwidth = frontend.BANDWIDTH_7_MHZ
		p.code_rate_HP = frontend.FEC_2_3
		p.code_rate_LP = frontend.FEC_NONE
		p.modulation = frontend.QAM_64
		p.constellation = frontend.QAM_64
		p.transmission_mode = frontend.TRANSMISSION_MODE_8K
		p.guard_interval = frontend.GUARD_INTERVAL_1_8
		return p
		
	def get_vpid(self, key):
		return int(self.channels[key][10])
		
	def get_apid(self, key):
		return int(self.channels[key][11])
		
	def printChannels(self):
		"""
		Print out all the stored channels
		"""
		for c in self.channels:
			print self.channels[c]
